package mydessertstore.web.servlet;

import mydessertstore.domain.*;
import mydessertstore.service.CatalogService;
import mydessertstore.service.LogService;
import mydessertstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class ConfirmOrderFormServlet extends HttpServlet {

    private static final String CONFIRM_ORDER_FORM = "/WEB-INF/jsp/order/confirmOrder.jsp";
    private static final String SHIPPING_FORM = "/WEB-INF/jsp/order/shippingForm.jsp";


    private String shippingAddressRequired;
    private List<Order> orderList;

    private OrderService orderService;
    private Order order;
    private Cart cart;

    private String shipToFirstName;
    private String shipToLastName;
    private String shipAddress1;
    private String shipAddress2;
    private String shipCity;
    private String shipState;
    private String shipZip;
    private String shipCountry;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        shippingAddressRequired = req.getParameter("shippingAddressRequired");
        order = new Order();

        HttpSession session = req.getSession();
        session.setAttribute("page", "confirmOrder");
        order = (Order)session.getAttribute("order");
        Account account = (Account)session.getAttribute("account");
        cart = (Cart) session.getAttribute("cart");

        if (shippingAddressRequired == null){

            //日志功能
            if(account!=null) {

                LogService logService = new LogService();

                //大家根据自己servlet完成的功能把"查看购物车"这里的字符串改成对应的功能
                String logInfo = logService.logInfo(" ") + " 确认生成订单 " ;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            else{
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + " 确认生成订单 " ;
                logService.insertLogInfo("未登录用户", logInfo);
            }
            //日志结束

            req.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(req, resp);
        }
        else{
//            shippingAddressRequired = null;

            //日志功能
            if(account!=null) {

                LogService logService = new LogService();

                //大家根据自己servlet完成的功能把"查看购物车"这里的字符串改成对应的功能
                String logInfo = logService.logInfo(" ") + " 更改收货地址 " ;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            else{
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + " 更改收货地址 " ;
                logService.insertLogInfo("未登录用户", logInfo);
            }
            //日志结束s

            shipToFirstName = req.getParameter("shipToFirstName");
            shipToLastName = req.getParameter("shipToLastName");
            shipAddress1 = req.getParameter("shipAddress1");
            shipAddress2 = req.getParameter("shipAddress2");
            shipCity = req.getParameter("shipCity");
            shipState = req.getParameter("shipState");
            shipZip = req.getParameter("shipZip");
            shipCountry = req.getParameter("shipCountry");


            order.setShipToFirstName(shipToFirstName);
            order.setShipToLastName(shipToLastName);
            order.setShipAddress1(shipAddress1);
            order.setShipAddress2(shipAddress2);
            order.setShipCity(shipCity);
            order.setShipState(shipState);
            order.setShipZip(shipZip);
            order.setCourier(shipCountry);

            session.setAttribute("order", order);

            account = (Account) session.getAttribute("account");
            //日志功能
            if(account!=null) {

                LogService logService = new LogService();

                String logInfo = logService.logInfo(" ") + " 修改邮寄地址 " ;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            else{
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + " 修改邮寄地址 " ;
                logService.insertLogInfo("未登录用户", logInfo);
            }
            //日志结束

            req.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(req, resp);
        }

    }

}
